package com.share.example.selenium.service.hzhc.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.share.common.utils.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.internal.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.function.Function;

@Data
@Slf4j
@Service
public class HcxxUserService {
    public static void main(String[] args) {
        String s = listUsers();
        System.out.println(s);
    }

    public static List<JSONObject> parseFileData() {
        List<JSONObject> reList = new LinkedList<>();
        List<String> strList = FileUtil.readByLine("D:\\project\\we-share\\share-example\\share-example-selenium\\src\\main\\java\\com\\share\\example\\selenium\\schedule\\a.txt");
        for (String str : strList) {
            if (StringUtil.isBlank(str)) {
                continue;
            }
            List<JSONObject> tmpList = parseData(str);
            if (!CollectionUtils.isEmpty(tmpList)) {
                reList.addAll(tmpList);
            }
        }
        return reList;
    }

    public static List<JSONObject> parseData(String str) {
        List<JSONObject> reList = new LinkedList<>();
        String s = JSONPath.eval(str, "$.data").toString();
        JSONArray objects = JSON.parseArray(s);
        for (Object object : objects) {
            try {
                JSONObject jsonObject = (JSONObject) object;
                String name = jsonObject.getString("name");
                String image = jsonObject.getString("image");
                String jobNumber = jsonObject.getString("jobNumber");
                if (image == null || image.length() == 0) {
                    continue;
                }
                String s1 = "_" + PinYinUtil.spellFirstChar(name);
                String finalName = "/cimg/" + jobNumber + s1 + ".jpg";
                byte[] bytes = HttpUtil.downFile(image, null);

                if (bytes.length > 100) {
                    MinioUtil.uploadFile(finalName, bytes);
                }

                JSONObject obj = new JSONObject();
                obj.put("jobNumber", jobNumber);
                obj.put("name", name);
                obj.put("image", image);
                reList.add(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return reList;
    }

    /**
     * 下载数据
     *
     * @return
     */
    public static String listUsers() {
        Executor executorService = ThreadUtil.getExecutor();

        String url = "https://mh.ihanchen.com/api/employee/query";
        String token = "116e45aacde346e3b472c66e0c9d7342";

        Map<String, String> header = new HashMap<>();
        header.put("token", token);

        JSONObject param = new JSONObject();
        JSONObject conditionVo = new JSONObject();
        conditionVo.put("deptType", 0);
        param.put("conditionVo", conditionVo);
        param.put("_t", System.currentTimeMillis());
        param.put("pageNum", 1);
        param.put("pageSize", 20); //显示条数,注释该行不填写将使用默认10,最大20
        Function<Integer, String> f1 = (pageNo) -> {
            param.put("pageNum", pageNo);//默认第一页
            try {
                return HttpUtil.post(url, param.toString(), header);
            } catch (Exception e) {
                log.error("调用异常", e);
                return null;
            }
        };

        //合并处理
        Function<List<String>, String> f2 = (list) -> {
            JSONObject obj = new JSONObject();
            List<Object> pageList = new LinkedList<>();
            if (!CollectionUtils.isEmpty(list)) {
                for (String item : list) {
                    //数据的合并处理
                    List<Object> read = (List<Object>) JSONPath.read(item, "$.data.data");
                    pageList.addAll(read);
                }
                obj.put("totalPage", JSONPath.read(list.get(0), "$.data.totalPage"));
                obj.put("code", JSONPath.read(list.get(0), "$.code"));
            }
            obj.put("data", pageList);
            return obj.toString();
        };
        String s = HttpUtil.pageAll("$.data.totalPage", f1, f2, executorService);
        return s;
    }
}
